package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import com.utils.OtherUtils;

/**
 * DAO
 */
public class DAO {

	private static String dbURL = "jdbc:db2://localhost:50000/BFHEAD";
	private static String dbUser = "db2admin";
	private static String dbPwd = "db2@dmin";
	
	private Connection connection;
	
	public DAO(){
		connection = startDBDriver();
	}
	
	public Connection getConnection() {
		return connection;
	}
	
	private Connection startDBDriver() {
		try {
			OtherUtils otherUtils = new OtherUtils();
			Properties properties = otherUtils.loadPropertiesFile();
			String driver = "com.ibm.db2.jcc.DB2Driver";

			if (properties.get("driver")!=null)
				driver = (String)properties.get("driver");
			
			Class.forName(driver);
			
			if (properties.get("dbURL")!=null)
				dbURL = (String)properties.get("dbURL");
			
			if (properties.get("dbUser")!=null)
				dbUser = (String)properties.get("dbUser");
			
			if (properties.get("dbPwd")!=null)
				dbPwd = (String)properties.get("dbPwd");
			
			return DriverManager.getConnection(dbURL, dbUser, dbPwd);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
